package de.slothsoft.dome;

import java.util.Date;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import de.slothsoft.dome.core.Todo;
import de.slothsoft.dome.database.TodosDataSource;

/**
 * A fragment representing a single Task detail screen. This fragment is either
 * contained in a {@link TaskListActivity} in two-pane mode (on tablets) or a
 * {@link TaskDetailActivity} on handsets.
 */
public class TaskDetailFragment extends Fragment {
	/**
	 * The fragment argument representing the item ID that this fragment
	 * represents.
	 */
	public static final String ARG_ITEM_ID = "item_id";

	private TodosDataSource datasource;
	private Todo mItem;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		datasource = new TodosDataSource(getActivity());
		datasource.open();

		if (getArguments().containsKey(ARG_ITEM_ID)) {
			// Load the dummy content specified by the fragment
			// arguments. In a real-world scenario, use a Loader
			// to load content from a content provider.
			long id = getArguments().getLong(ARG_ITEM_ID);
			if (id != -1) {
				mItem = datasource.getTodo(getArguments().getLong(ARG_ITEM_ID));
			}
			hookButtonListener();
		}
	}

	private void hookButtonListener() {
		Button okButton = (Button) getActivity().findViewById(R.id.ok);
		okButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				peformSave();
			}
		});
	}

	protected void peformSave() {
		if (mItem == null) {
			performCreateSave();
		} else {
			performUpdateSave();
		}
		NavUtils.navigateUpTo(getActivity(), new Intent(getActivity(),
				TaskListActivity.class));
	}

	private void performCreateSave() {
		Todo newItem = new Todo();
		updateItemFromControls(newItem);
		mItem = datasource.createTodo(newItem);
	}

	private void performUpdateSave() {
		updateItemFromControls(mItem);
		datasource.updateTodo(mItem);
	}

	private void updateItemFromControls(Todo item) {
		item.setName(getNameText().getText().toString());
		item.setDescription(getDescriptionText().getText().toString());
	}

	@Override
	public void onDestroy() {
		datasource.close();
		super.onDestroy();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_task_detail,
				container, false);

		// Show the dummy content as text in a TextView.
		if (mItem != null) {
			getNameText().setText(mItem.getName());
			getDescriptionText().setText(mItem.getDescription());
		} else {
			getNameText().setText("New Task");
			getDescriptionText().setText("Task created on " + new Date());
		}

		return rootView;
	}

	private EditText getNameText() {
		return ((EditText) getActivity().findViewById(R.id.name));
	}

	private EditText getDescriptionText() {
		return ((EditText) getActivity().findViewById(R.id.description));
	}
}
